package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ym.mapper.RolePermissionMapper;
import ym.pojo.RolePermission;
import ym.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void add(RolePermission rolePermission) {
        rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public void update(RolePermission rolePermission) {
        rolePermissionMapper.updateById(rolePermission);
    }

    @Override
    public void delete(Integer id) {
        rolePermissionMapper.deleteById(id);
    }

    @Override
    public RolePermission findById(Integer id) {
        return rolePermissionMapper.selectById(id);
    }

    @Override
    public List<RolePermission> findAll() {
        return rolePermissionMapper.selectList(null);
    }

    @Override
    public Page<RolePermission> search(String search, int page, int size) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        if (search != null) {

        }
        return rolePermissionMapper.selectPage(new Page(page, size), queryWrapper);
    }

    @Override
    public void deleteByRoleId(Integer roleId) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleId", roleId);
        rolePermissionMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByPermissionId(Integer permissionId) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("permissionId", permissionId);
        rolePermissionMapper.delete(queryWrapper);

    }
}
